// JavaScript Document

var currdate= new Date()
var month= currdate.getMonth()+1;
var day= currdate.getDate();
var year= currdate.getFullYear();


var r={
  'special':/[\W]/g,
  'quotes':/['\''&'\"']/g,
  'notnumbers':/[^\d]/g
}
function valid(o,w){
  o.value = o.value.replace(r[w],'');
}
//username exists validation

pic1 = new Image(16, 16); 
pic1.src = "loader.gif";

$(document).ready(function(){

$("#username").change(function() { 

var usr = $("#username").val();


if(usr.length >= 4)
{
$("#status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');

    $.ajax({  
    type: "POST",  
    url: "check.php",  
    data: "username="+ usr,  
    success: function(msg){  
   
   $("#status").ajaxComplete(function(event, request, settings){ 

	if(msg == 'OK')
	{ 
        $("#username").removeClass('object_error'); // if necessary
		$("#username").addClass("object_ok");
		$(this).html('&nbsp;<img src="images/tick.gif" align="absmiddle">');
	}  
	else  
	{  
		$("#username").removeClass('object_ok'); // if necessary
		$("#username").addClass("object_error");
		$(this).html(msg);
	}  
   
   });

 } 
   
  }); 

}
else
	{
	$("#status").html('<font color="red">The username should have at least <strong>4</strong> characters.</font>');
	$("#username").removeClass('object_ok'); // if necessary
	$("#username").addClass("object_error");
	}

});

});




//ends validation
function signvalidate()
{
	//form name is register 
if (document.register.username.value =="")
{
document.register.username.focus();
alert("You must enter a username.");
return (false);
}


if (document.register.password.value =="")
{
document.register.password.focus();
alert("You must enter password.");
return (false);
}
if (document.register.confirm_password.value =="")
{
document.register.confirm_password.focus();
alert("You must re-enter password");
return (false);
}

if (document.register.password.value !==document.register.confirm_password.value)
{
document.register.confirm_password.focus();
alert("You must enter same password");
return (false);

}
if (document.register.name.value =="")
{
document.register.name.focus();
alert("You must enter a name.");
return (false);
}



// require at least one radio button be selected
var radioSelected = false;
for (i = 0;  i < document.register.gender.length;  i++)
{
if (document.register.gender[i].checked)
radioSelected = true;
}
if (!radioSelected)
{
alert("Please select one of the \"Gender\" options.");
return (false);
}



//check if age is left empty
if (document.register.age.value =="")
{
document.register.age.focus();
alert("You must enter age.");
return (false);
}
//check if age has numbers
var num=document.register.age.value;
if(isNaN(num))
{
  	document.register.age.focus();
	alert("Age should contain only numbers");
	return (false);
}


//check if passport is left empty
if (document.register.passport.value =="")
{
document.register.passport.focus();
alert("You must enter passport no.");
return (false);
}

if (document.register.nationality.value =="")
{
document.register.nationality.focus();
alert("You must enter nationality.");
return (false);
}


// check if email field is blank
if (document.register.email.value == "")
{
alert("Please enter a value for the \"Email\" field.");
document.register.email.focus();
return (false);
}

}

function validate()
{
	

var todaydate=parseInt(day)+(parseInt(month)*31)+(parseInt(year)*365);
var dd=parseInt(document.booking1.td.value);
var mm=parseInt(document.booking1.tm.value);
var yy=parseInt(document.booking1.ty.value);
var entered=parseInt(dd)+(parseInt(mm)*31)+(parseInt(yy)*365);
var vnv=parseInt(document.booking1.veg.value)+parseInt(document.booking1.non_veg.value);

if (document.booking1.tripname.value =="--Select--")
{
	alert("please select a tour");
	return(false);
}

if (document.booking1.no_of_pass.value!=vnv)
{
	alert("please enter the correct no. of passengers/ no. of veg an non_veg passengers, eg: if no. of passengers=5, then veg=2, nonveg=3, the number should match with meal preferences total");
	return(false)
}

if(entered<todaydate)
{
   alert("please enter a valid travel date");
   return(false);	
}

document.booking1.tripcode.disabled=false;
alert("Thank you for your booking, Please provide the payment method and details in the page that you will be redirected");


}
function chknum()
{
	var e = event || evt; 
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function getcode(tripname)
{
 var xhr = new XMLHttpRequest();


  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
      var code = xhr.responseText;     
	       document.booking1.tripcode.value =code;
      
    }
  }
  xhr.open("GET", "getcode.php?tripname=" + tripname,true);
  xhr.send(null);
	
}

function getcode1(tripcode)
{
 var xhr1 = new XMLHttpRequest();


  xhr1.onreadystatechange = function () {
    if (xhr1.readyState == 4) {
      var code = xhr1.responseText;     
	     var tour = code.split(': ');
      document.admintours.tripname.value = tour[0];
     document.admintours.days.value = tour[1];
	 document.admintours.cost.value= tour[2];
	 document.admintours.available.value= tour[3];  
    }
  }
  xhr1.open("GET", "getcode1.php?tripcode=" + tripcode,true);
  xhr1.send(null);
	
}


function adtourvalidate()
{
 
 if(document.admintours.tripcode.value=="--Select--")
 {
	 document.admintours.tripcode.focus();
	 alert("you must select a trip code");
	 return (false);
 }
 if (document.admintours.tripname.value =="")
{
document.admintours.tripname.focus();
alert("You must enter a tripname");
return (false);
}



if (document.admintours.days.value == "")
{
alert("You must enter number of days");
document.admintours.days.focus();
return (false);
}	
if (document.admintours.cost.value =="")
{
document.admintours.cost.focus();
alert("You must enter cost.");
return (false);
}



if (document.admintours.available.value == "")
{
alert("Enter the availability constraint");
document.admintours.available.focus();
return (false);
}
}







function insertvalidate()
{
	 if(document.addtours.tripcode.value=="")
 {
	 document.addtours.tripcode.focus();
	 alert("you must enter a trip code");
	 return (false);
 }
 if (document.addtours.tripname.value =="")
{
document.addtours.tripname.focus();
alert("You must enter a tripname");
return (false);
}



if (document.addtours.days.value == "")
{
alert("You must enter number of days");
document.addtours.days.focus();
return (false);
}	
if (document.addtours.cost.value =="")
{
document.addtours.cost.focus();
alert("You must enter cost.");
return (false);
}



if (document.addtours.available.value == "")
{
alert("Enter the availability constraint");
document.addtours.available.focus();
return (false);
}
}